<?php

namespace App\Admin\Controllers;

use App\Models\Count;
use App\Models\User;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户记录统计';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $grid->column('id', __('Id'));
        $grid->column('username', __('用户名'));
        $grid->column('created_at', __('注册时间'));
        $grid->column('taking_time', __('开始日期'))->display(function ($taking_time) {
            return date('Y/m/d', strtotime($taking_time));
        });
        $grid->column('day', __('今日天数'))->display(function ($taking_time) {
            $datetime_start = new \DateTime(Carbon::now());
            $datetime_end = new \DateTime($taking_time);
            $days = $datetime_start->diff($datetime_end)->days+1;
            return '第'.$days.'天';
        });
        $grid->column('nausea', __('今日恶心次数'))->display(function () {
            return Count::where('user_id', $this->id)->whereDate('updated_at', $this->updated_at->format('Y-m-d'))->value('nausea') ?? 0;
        });
        $grid->column('vomit', __('今日呕吐次数'))->display(function () {
            return Count::where('user_id', $this->id)->whereDate('updated_at', $this->updated_at->format('Y-m-d'))->value('vomit') ?? 0;
        });

        $grid->disableColumnSelector();
        $grid->disableBatchActions();
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('username', '用户名');
        });
        $grid->actions(function ($actions) {
            $actions->disableEdit();
            $actions->disableDelete();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
//        $show = new Show(User::findOrFail($id));
//
//        $show->field('id', __('Id'));
//        $show->field('username', __('用户名'));
//        $show->field('password', __('密码'));
//        $show->field('created_at', __('注册时间'));
        $date_info    = [];
        $date_info[]  = '类型\\日期';
        $count_info   = [];
        $count_info[] = ['恶心次数'];
        $count_info[] = ['呕吐次数'];
        $user         = User::findOrFail($id);
        $taking_day          = date('Y/m/d', strtotime($user->taking_time));
        //用户服药7天的统计
        $counts = Count::where('user_id', $id)->where('updated_at','>=',$user->taking_time)->limit(7)->get();
        foreach ($counts as $key=>$count){
            $date_info[] = '第' . ($key+1) . '天(' . date('Y/m/d', strtotime($count->updated_at)) . ')';
            array_push($count_info[0], $count->nausea);
            array_push($count_info[1], $count->vomit);
        }

       // $count        = Count::where('user_id', $id)->whereDate('updated_at',date('Y-m-d', strtotime($user->taking_time)))->first();

//        if($count){
//            $date_info[1] = '第1天(' . $taking_day . ')';
//            array_push($count_info[0], $count->nausea);
//            array_push($count_info[1], $count->vomit);
//            for ($i = 2; $i <= 6; $i++) {
//                $s_day         = date('Y/m/d', strtotime($count->updated_at->addDay($i - 1)));
//                $date_info[$i] = '第' . $i . '天(' . $s_day . ')';
//                $s_count       = Count::where('user_id', $id)->whereDate('updated_at', $s_day)->first();
//                if ($s_count) {
//                    $s_nausea = $s_count->nausea;
//                    $s_vomit  = $s_count->vomit;
//                } else {
//                    $s_nausea = $s_vomit = 0;
//                }
//
//                array_push($count_info[0], $s_nausea);
//                array_push($count_info[1], $s_vomit);
//            }
//        }



        $show = new Table($date_info, $count_info);
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->text('username', __('Username'));
        $form->password('password', __('Password'));

        return $form;
    }
}
